package com.qyhl.webtv.module_live.teletext.content;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.qyhl.webtv.commonlib.entity.live.TeleTextLiveBean;
import com.qyhl.webtv.commonlib.utils.view.ViewPageFragment;
import com.qyhl.webtv.module_live.R;
import com.qyhl.webtv.module_live.R2;
import com.qyhl.webtv.module_live.common.TeleTextInterface;
import com.qyhl.webtv.module_live.utils.ItemViewDelegete.MoreIconItemDelegete;
import com.qyhl.webtv.module_live.utils.ItemViewDelegete.NanBuIconItemDelegete;
import com.qyhl.webtv.module_live.utils.ItemViewDelegete.NanBuVideoItemDelegete;
import com.qyhl.webtv.module_live.utils.ItemViewDelegete.RadioItemDelegete;
import com.qyhl.webtv.module_live.utils.ItemViewDelegete.SingleIconItemDelegete;
import com.qyhl.webtv.module_live.utils.ItemViewDelegete.VideoItemDelegete;
import com.qyhl.webtv.module_live.utils.MediaListManager;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import es.dmoral.toasty.Toasty;


/**
 * Created by qyhl on 2017/7/6.
 * 图文直播 报道界面
 */
public class TeleTextLiveFragment extends ViewPageFragment implements TeleTextLiveContract.TeleTextLiveView, TeleTextInterface.TeleTextLive {

    @BindView(R2.id.teletext_listview)
    RecyclerView teletextRecycleView;
    @BindView(R2.id.live_refresh)
    SmartRefreshLayout liveRefresh;
    @BindView(R2.id.live_layout)
    LoadingLayout liveLayout;


    private String sceneId;
    private TeleTextLiveContract.TeleTextLivePresenter mPresenter;
    private List<TeleTextLiveBean> mBeanList = new ArrayList<>();
    private MultiItemTypeAdapter mAdapter;

    private HeaderAndFooterWrapper mHeaderAndFooterWrapper;

    private int contentId = 0;

    private String type;

    private boolean isNanBu;

    public void setNanBu(boolean nanBu) {
        isNanBu = nanBu;
    }

    public void setType(String type) {
        this.type = type;
    }

    public TeleTextLiveFragment() {

    }

    public static TeleTextLiveFragment newInstance(String sceneId, String type) {
        TeleTextLiveFragment fragment = new TeleTextLiveFragment();
        fragment.setSceneId(sceneId);
        fragment.setType(type);
        fragment.setNanBu(false);
        return fragment;
    }

    public static TeleTextLiveFragment newInstance(String sceneId, String type, boolean isNanBu) {
        TeleTextLiveFragment fragment = new TeleTextLiveFragment();
        fragment.setSceneId(sceneId);
        fragment.setType(type);
        fragment.setNanBu(isNanBu);
        return fragment;
    }

    public void setSceneId(String sceneId) {
        this.sceneId = sceneId;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.live_fragment_teletext_live, container, false);
            ButterKnife.bind(this, rootView);
        }
        init();//初始化
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }

        return rootView;
    }

    //初始化
    private void init() {
        teletextRecycleView.setNestedScrollingEnabled("0".equals(type));

        liveLayout.setStatus(LoadingLayout.Loading);
        mPresenter = new TeleTextLivePresenter(this);

        //配置刷新
        liveRefresh.setRefreshHeader(new MaterialHeader(getActivity()));
        liveRefresh.setRefreshFooter(new ClassicsFooter(getActivity()));
        //加载时无法操作内容
        liveRefresh.setDisableContentWhenLoading(true);
        //刷新时无法操作内容
        liveRefresh.setDisableContentWhenRefresh(true);
        liveRefresh.setEnableScrollContentWhenLoaded(true);
        liveRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                contentId = 0;
                mPresenter.getDatas(sceneId, "0", false);
            }
        });

        liveRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                mPresenter.getDatas(sceneId, contentId + "", true);
            }
        });

        //初始化recycleview和适配器
        teletextRecycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        teletextRecycleView.addItemDecoration(new DividerItemDecoration(getContext(), 0));


        mAdapter = new MultiItemTypeAdapter(getContext(), mBeanList);
        if (isNanBu) {
            mAdapter.addItemViewDelegate(new NanBuIconItemDelegete(getContext(), mBeanList));
            mAdapter.addItemViewDelegate(new NanBuVideoItemDelegete(getContext(), mBeanList));
        } else {
            mAdapter.addItemViewDelegate(new MoreIconItemDelegete(getContext(), mBeanList));
            mAdapter.addItemViewDelegate(new SingleIconItemDelegete(getContext(), mBeanList));
            mAdapter.addItemViewDelegate(new VideoItemDelegete(getContext(), mBeanList));
        }

        mAdapter.addItemViewDelegate(new RadioItemDelegete(getContext(), mBeanList));

        if ("0".equals(type)) {
            teletextRecycleView.setAdapter(mAdapter);
        } else {
            mHeaderAndFooterWrapper = new HeaderAndFooterWrapper(mAdapter);
            ImageView imageView = new ImageView(getContext());
            imageView.setBackgroundResource(R.color.white);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500));
            mHeaderAndFooterWrapper.addFootView(imageView);
            teletextRecycleView.setAdapter(mHeaderAndFooterWrapper);
        }

        liveLayout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                mPresenter.getDatas(sceneId, "0", false);
            }
        });


        liveRefresh.autoRefresh();

    }


    @Override
    public void setDatas(List<TeleTextLiveBean> beanList, boolean isMore) {
        liveRefresh.finishLoadMore();
        liveRefresh.finishRefresh();
        liveLayout.setStatus(LoadingLayout.Success);
        if (isMore) {
            mBeanList.addAll(beanList);
        } else {
            mBeanList.clear();
            mBeanList.addAll(beanList);
        }
        contentId = beanList.get(beanList.size() - 1).getId();
        if ("0".equals(type)) {
            mAdapter.notifyDataSetChanged();
        } else {
            mHeaderAndFooterWrapper.notifyDataSetChanged();
        }

    }

    @Override
    public void setError(String msg) {
        liveRefresh.finishLoadMore();
        liveRefresh.finishRefresh();
        liveLayout.setStatus(LoadingLayout.Error);
    }

    @Override
    public void setEmpty(String msg) {
        liveRefresh.finishRefresh();
        liveRefresh.finishLoadMore();
        liveLayout.setStatus(LoadingLayout.Empty);
    }

    @Override
    public void setNoMore(String msg) {
        liveRefresh.finishLoadMore(true);//设置全部加在完成，将无法触发上拉加载
        showMessage(msg);
    }


    @Override
    public void showMessage(String msg) {
        liveRefresh.finishRefresh();
        liveRefresh.finishLoadMore();
        Toasty.normal(getContext(), msg + "").show();
    }


    @Override
    public void doRefresh() {
        RecyclerView.LayoutManager layoutManager = teletextRecycleView.getLayoutManager();
        if (layoutManager instanceof LinearLayoutManager) {
            LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
            linearManager.scrollToPositionWithOffset(0, 0);
            linearManager.setStackFromEnd(true);
        }
        liveRefresh.autoRefresh();//自动刷新
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {

        } else {
            MediaListManager me = MediaListManager.getIntance();
            if (me.isPlay()) {
                //停止播放语音
                me.release();

                //停止动画
                me.getTextView().endAnimation();

                //恢复时间
                me.getTextView().setContent(me.getContentBean().getAudioDuration());
            }
        }
    }

}
